from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict
# 导入类视图
from django.views import View

# from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib, urllib, base64, hmac
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from django_test.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from django_test.settings import BASE_DIR

# 导包
from django.db.models import Q, F

from django.utils.decorators import method_decorator

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

# 导入数据库
from .models import *

# 序列化
from .ser import *

# 导包
import redis

# Create your views here.


class TypeAdmin(APIView):

    def get(self,request):
        res = {}
        res['code'] = 200
        res['data'] = TypeSer(Type.objects.all(), many=True).data
        return Response(res)

    def post(self, request):
        res = {}
        type = request.data.get('type')
        if not(Type.objects.filter(type= type)):
            Type(type=type).save()
            res['code'] = 200
            res['message'] = '添加成功'
        else:
            res['code'] = 403
            res['message'] = '类型已存在'
        return Response(res)

class TestAdmin(APIView):
    def get(self, request):
        type_id = request.GET.get('type_id')[0]
        is_right = request.GET.get('is_right')
        if not(type_id):
            type_id = 1
        type_id = int(type_id)
        res = {}
        res['code'] = 200
        res['data'] = TestSer(Test.objects.filter(type_id= type_id, is_delete=0).exclude(is_right=is_right), many=True).data
        return Response(res)

    def post(self, request):
        res = {}
        title  = request.data.get('title')
        if not(Test.objects.filter(question = title)):
            Test(question=title,  answer= request.data.get('answer'), type_id=request.data.get('type')).save()
            res['code'] = 200
            res['message'] = '添加成功'
        else:
            res['code'] = 403
            res['message'] = '题目已存在'
        return Response(res)

    def put(self, request):
        res = {}
        put = QueryDict(request.body)
        put_str = list(put.items())[0][0]
        put_dict = json.loads(put_str)
        test = Test.objects.get(id=(put_dict.get('id')))
        test.question = put_dict.get('question')
        test.type_id = put_dict.get('type')
        test.answer = put_dict.get('answer')
        test.save()
        res['code'] = 200
        res['message']  = '修改成功'
        return Response(res)

    def delete(self, request):
        res = {}
        delete = QueryDict(request.body)
        delete_str = list(delete.items())[0][0]
        delete_dict = json.loads(delete_str)
        print(delete_dict)
        test = Test.objects.get(id= delete_dict.get('id'))
        if test.is_delete == 0:
            test.is_delete = 1
        else:
            test.is_delete = 0
        test.save()
        return Response(res)

class Pass(APIView):
    def post(self, request):
        res = {}
        test = Test.objects.get(id=request.data.get('id'))
        if test.is_right == 0:
            test.is_right = 1
        else:
            test.is_right = 0
        test.save()
        return Response(res)